package cn.cdeden.job.snailjob.snailjob;

import cn.cdeden.common.core.utils.StringUtils;
import cn.cdeden.knowledge.api.domain.RemoteQaFilesTreeService;
import cn.cdeden.knowledge.api.domain.vo.RemoteQaFilesTreeVo;
import cn.cdeden.resource.api.RemoteFileService;
import cn.hutool.core.util.ObjectUtil;
import com.aizuda.snailjob.client.job.core.dto.JobArgs;
import com.aizuda.snailjob.client.job.core.executor.AbstractJobExecutor;
import com.aizuda.snailjob.client.model.ExecuteResult;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
public class PersonalJobExecutor extends AbstractJobExecutor {

    @DubboReference(check = false)
    private final RemoteFileService remoteFileService;

    @DubboReference(check = false)
    private final RemoteQaFilesTreeService remoteQaFilesTreeService;

    @Override
    protected ExecuteResult doJobExecute(JobArgs jobArgs) {
        System.out.println("=====================================================" + "测试定时删除AI文档中用户上传的文件");
        List<RemoteQaFilesTreeVo> remoteQaFilesTreeVoList = remoteQaFilesTreeService.getAllQaFiles();
        if (ObjectUtil.isEmpty(remoteQaFilesTreeVoList)) {
            return ExecuteResult.success("删除成功");
        }
        List<Long> ossIds = remoteQaFilesTreeVoList.stream().map(RemoteQaFilesTreeVo::getStorageId).map(Long::valueOf).collect(Collectors.toList());

        remoteFileService.deleteWithValidByIds(ossIds, true);

        remoteQaFilesTreeService.deleteAllQaFiles();

        return ExecuteResult.success("删除成功");

    }
}
